﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace FHJ.FHPI.PushNotifications.Library
{
    /// <summary>
    /// Class for making Http requests.
    /// </summary>
    public static class Http
    {
        /// <summary>
        /// Makes a Http POST request to a given Url and returns the response.
        /// </summary>
        /// <param name="url">The Url to which to post.</param>
        /// <param name="pairs">The parameters for the request.</param>
        /// <returns>The raw byte response to the request.</returns>
        public static Byte[] Post(String url, NameValueCollection pairs)
        {
            Byte[] response = null;
            using (WebClient client = new WebClient())
            {
                response = client.UploadValues(url, pairs);
            }
            return response;
        }

        /// <summary>
        /// Makes a (Async) Http POST request to a given Url and returns the response.
        /// </summary>
        /// <param name="url">The Url to which to post.</param>
        /// <param name="pairs">The parameters for the request.</param>
        /// <param name="requestId">Identifier object for the request.</param>
        /// <param name="eventHandler">Event handler which should be called when the request has completed.</param>
        public static void PostAsync(String url, NameValueCollection pairs, Object requestId, UploadValuesCompletedEventHandler eventHandler,
            NameValueCollection headers = null)
        {
            using (WebClient client = new WebClient())
            {
                if (!(headers == null))
                {
                    client.Headers.Add(headers);
                }

                client.UploadValuesCompleted += eventHandler;
                client.UploadValuesAsync(new System.Uri(url), "POST", pairs, requestId);
            }
        }
    }
}
